﻿using System;
using System.Text;
using System.Diagnostics;
using IWshRuntimeLibrary;
using System.Runtime.InteropServices;

namespace Sinacor.OMM.Util
{
    public class Windows
    {
        #region [ Import Method ]

        [DllImport("shell32.dll")]
        private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);

        #endregion

        #region [ Enum ]

        public enum SpecialFolder
        {
            Desktop = 0x0019,
            StartMenu = 0x0017,
            ApplicationData = 0x0023
        }

        #endregion

        #region [ Public Methods ]

        public static int RunProcess(string fileName, string arguments)
        {
            return RunProcess(fileName, arguments, null, null, 0);
        }

        public static int RunProcess(string fileName)
        {
            return RunProcess(fileName, null, null, null, 0);
        }

        public static int RunProcess(string fileName, string arguments, int timeOut)
        {
            return RunProcess(fileName, arguments, null, null, timeOut);
        }

        public static int RunProcess(string fileName, string arguments, DataReceivedEventHandler errorDataReceived, DataReceivedEventHandler outputDataReceived, int timeOut)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = fileName;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardOutput = true;

            if (!string.IsNullOrEmpty(arguments))
                p.StartInfo.Arguments = arguments;

            if (errorDataReceived != null)
                p.ErrorDataReceived += errorDataReceived;

            if (outputDataReceived != null)
                p.OutputDataReceived += outputDataReceived;

            p.Start();

            p.BeginOutputReadLine();
            p.BeginErrorReadLine();

            if (timeOut > 0)
                p.WaitForExit(timeOut);
            else
                p.WaitForExit();

            return p.ExitCode;
        }

        public static int KillProcess(string processName)
        {
            return RunProcess("cmd.exe", string.Format("/c taskkill /F /IM \"{0}.exe\"", processName));
        }

        public static void CreateShortcut(string pathLink, string iconLocation, string targetPath)
        {
            CreateShortcut(pathLink, iconLocation, targetPath, string.Empty);
        }

        public static void CreateShortcut(string pathLink, string iconLocation, string targetPath, string arguments)
        {
            IWshShortcut link;
            WshShell shell = new WshShell();

            link = (IWshShortcut)shell.CreateShortcut(pathLink);

            if (!string.IsNullOrEmpty(iconLocation))
            {
                link.IconLocation = iconLocation;
                link.Arguments = arguments;
            }

            link.TargetPath = targetPath;
            link.WorkingDirectory = System.IO.Path.GetDirectoryName(targetPath);
                        
            link.Save();
        }

        public static string GetFolderPath(SpecialFolder specialFolder)
        {
            StringBuilder path = new StringBuilder(260);

            SHGetSpecialFolderPath(IntPtr.Zero, path, (int)specialFolder, true);

            return path.ToString();
        }

        #endregion
    }
}
